﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Giraffe.Core.Common;
using Giraffe.Core.Common.Messengers;

namespace Giraffe.Core.Model
{
    public class User : IUser
    {
        private readonly IEnumerable<IMessenger> messengers;
        private IConversationManager _conversationManager;
        private List<IContact> _contacts;
        public event System.Action MessengerContactListChanged;

        public User(IEnumerable<IMessenger> messengers)
        {
            this.messengers = messengers;

            var ownContacts = new List<IMessengerContact>();
            foreach (IMessenger messenger in this.messengers)
            {
                ownContacts.Add(messenger.OwnContact);
                messenger.Logined += OnMessengerLogined;
                messenger.ContactListChanged += OnMessengerContactListChanged;
            }

            OwnContact = new Contact(ownContacts);
        }

        public IEnumerable<IMessenger> Messengers
        {
            get { return messengers; }
        }

        public IEnumerable<IContact> Contacts
        {
            get
            {
                //todo: change to multimessnger contacts. List should be updated when contacts collection changed.
                if (_contacts == null)
                {
                    UpdateContacts();
                }
                return _contacts;
            }
        }
     
        public IContact OwnContact
        { 
            get; private set;

        }

        public IConversationManager ConversationManager
        {
            get
            {
                if (_conversationManager == null)
                {
                    _conversationManager = ObjectProvider.Resolve<IConversationManager>();
                }
                return _conversationManager;
            }
        }

        public virtual void Login()
        {
            foreach (var messenger in Messengers)
            {
                messenger.Login();
            }

        }

        public void Logout()
        {
            foreach (var messenger in Messengers)
            {
                messenger.Logout();
            }

        }

        private void OnMessengerContactListChanged()
        {
            UpdateContacts();
            if (MessengerContactListChanged != null)
            {
                MessengerContactListChanged();
            }
        }

        private void UpdateContacts()
        {
            _contacts = new List<IContact>();
            foreach (var messenger in messengers)
            {
                foreach (var messengerContact in messenger.ContactList)
                {
                    _contacts.Add(new Contact(new List<IMessengerContact> { messengerContact }));
                }
            }
        }

        private void OnMessengerLogined()
        {
            OnMessengerContactListChanged();
        }
    }
}
